package com.kfm.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Iterator;

@WebServlet(value = "/form")
public class FormServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("FormServlet 接受到了 GET 请求");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("FormServlet 接受到了 POST 请求");

        String method = req.getMethod();
        System.out.println("请求的方式是：" + method);

        String uri = req.getRequestURI();
        System.out.println("URI: " + uri);
        String url = req.getRequestURL().toString();
        System.out.println("URL: " + url);

        String protocol = req.getProtocol();
        System.out.println("协议： " + protocol);

        String addr = req.getRemoteAddr();
        int port = req.getLocalPort();
        int port1 = req.getRemotePort();
        int port2 = req.getServerPort();

        System.out.println("地址是： " + addr + ", 端口：" + port);
        System.out.println(port1 + " \t " + port2 );

        System.out.println("=============================");

        System.out.println(req.getHeader("user-agent"));

//        Enumeration<String> headerNames = req.getHeaderNames();
//
//        while (headerNames.hasMoreElements()) {
//            String element = headerNames.nextElement();
//            System.out.println(element + " :" + req.getHeader(element));
//        }
    }
}
